﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using PefStudio.Base;

namespace PefStudio.Windows
{
    class CreateService
    {
        internal static void CreateMenu(ContextMenuStrip contextMenuStrip, IEnumerable<IMenuItem> items, Form parentForm)
        {
            CreateMenu(contextMenuStrip.Items, items, parentForm);
            contextMenuStrip.Opening += ContextMenuStrip_Opening;
        }

        internal static void CreateMenu(ToolStripItemCollection toolStripItemCollection, IEnumerable<IMenuItem> items, Form parentForm)
        {
            var sortedExtensionItems = ExtensionsService.Sort(items);
            foreach (var extensionItem in sortedExtensionItems)
            {
                if (extensionItem.HasSeparator == true && toolStripItemCollection.Count > 0)
                {
                    var separator = new ToolStripSeparator();
                    toolStripItemCollection.Add(separator);
                    extensionItem.ToolStripSeparator = separator;
                }
                extensionItem.ParentForm = parentForm;

                var toolStripItem = extensionItem as ToolStripItem;
                if (toolStripItem != null) toolStripItemCollection.Add(toolStripItem);
            }
        }

        public static void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var contextMenuStrip = sender as ContextMenuStrip;
            if (contextMenuStrip == null) return;

            foreach (var item in contextMenuStrip.Items)
            {
                if (item is IMenuItem)
                {
                    var menuItem = item as IMenuItem;
                    menuItem.ConditionEvaluate();
                }
            }
        }

        public static void CreateStatusBar(ToolStripItemCollection toolStripItemCollection, IEnumerable<IStatusBarItem> items)
        {
            var sortedExtensionItems = ExtensionsService.Sort(items);
            foreach (var extensionItem in sortedExtensionItems)
            {
                if (extensionItem.HasSeparator == true && toolStripItemCollection.Count > 1)
                    toolStripItemCollection.Add(new ToolStripSeparator());

                var toolStripItem = extensionItem as ToolStripItem;
                if (toolStripItem != null) toolStripItemCollection.Add(toolStripItem);
            }
        }

        public static void CreateToolStrip(ToolStripItemCollection toolStripItemCollection, IEnumerable<IToolStripItem> items)
        {
            var sortedExtensionItems = ExtensionsService.Sort(items);
            foreach (var extensionItem in sortedExtensionItems)
            {
                if (extensionItem.HasSeparator == true && toolStripItemCollection.Count > 1)
                    toolStripItemCollection.Add(new ToolStripSeparator());

                var toolStripItem = extensionItem as ToolStripItem;
                if (toolStripItem != null) toolStripItemCollection.Add(toolStripItem);
            }
        }
    }
}
